package com.pms.utils;

//~--- non-JDK imports --------------------------------------------------------



import java.io.File;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.pms.platform.resource.ResourceManager;

import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JRCsvExporter;
import net.sf.jasperreports.engine.export.JRCsvExporterParameter;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.j2ee.servlets.ImageServlet;

public class ReportUtility {
    private static final String TRACE_ID = "REPORTUTILITY";

    public ReportUtility() {
    	System.out.println("In RU Constructor");
    }
    
    /**
	 * This method is used to generate the HTML Report using Jasper Report
	 * 
	 * @param map - Parameter for the Query to pass to Report
	 * @param reportFile - File object containing jasper file. 
	 * @param out - Print Writer Objects
	 * @param request - Request Objects
	 * @return - Returns JRHtmlExporter Object. 
	 * @throws Exception
	 */
	public JRHtmlExporter generateHTMLReport(HashMap<String ,Object> map, File reportFile ,PrintWriter out ,HttpServletRequest request) throws Exception{
		
		
		JasperReport jasperReport;
		JasperPrint jasperPrint;
		
		Connection con = null;
        JRHtmlExporter exporter = new JRHtmlExporter();
        HashMap imagesMap = new HashMap();
        
        try {
        	con = ResourceManager.getConnection();
        	
	        jasperReport = (JasperReport)JRLoader.loadObject(reportFile);
	        jasperPrint = JasperFillManager.fillReport(jasperReport, map ,con);
	        
	        request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_LIST_SESSION_ATTRIBUTE, jasperPrint);
	        request.getSession().setAttribute("IMAGES_MAP", imagesMap);
	        
	        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
	        exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
	        exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.TRUE);
	        exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap);
	        exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath() + "/image?image=");
	        exporter.setParameter(JRHtmlExporterParameter.SIZE_UNIT, "px");
	        
	        return exporter;
        }catch(Exception ex){ 
        	ex.printStackTrace();
        	throw ex;
        	
        }
        finally {
        	ResourceManager.freeConnection(con ,TRACE_ID);
        }
	}

    /**
	 * This method is used to generate the HTML Report using Jasper Report
	 * 
	 * @param map - Parameter for the Query to pass to Report
	 * @param reportFile - File object containing jasper file. 
	 * @param out - Print Writer Objects
	 * @param request - Request Objects
	 * @param subReportFile - File object containing subReport jasper file. 
	 * @return - Returns JRHtmlExporter Object. 
	 * @throws Exception
	 */
	public JRHtmlExporter generateHTMLReportWithSubReport(HashMap<String ,Object> map, File reportFile ,PrintWriter out ,HttpServletRequest request ,ArrayList<File> subReportFile) throws Exception{
		
		
		JasperReport jasperReport;
		JasperReport subReport;
		JasperPrint jasperPrint;
		
		Connection con = null;
        JRHtmlExporter exporter = new JRHtmlExporter();
        HashMap imagesMap = new HashMap();
        
        try {
        	con = ResourceManager.getConnection();
        	
	        jasperReport = (JasperReport)JRLoader.loadObject(reportFile);
	        if(subReportFile!=null && subReportFile.size()>0){
	        	for(int i=0;i<subReportFile.size();i++){
	        		
	        		File subReportFill = (File)subReportFile.get(i);
		        	
	        		subReport= (JasperReport)JRLoader.loadObject(subReportFill);
	    	        map.put("SubReport-"+i, subReport);
	    	        System.out.println("****************** subReportFile ==> "+subReport.getName());
	        	}
	        	
	        }
	        
	        jasperPrint = JasperFillManager.fillReport(jasperReport, map ,con);
	        
	        request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_LIST_SESSION_ATTRIBUTE, jasperPrint);
	        request.getSession().setAttribute("IMAGES_MAP", imagesMap);
	        
	        exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
	        exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
	        exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.TRUE);
	        exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap);
	        exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, request.getContextPath() + "/image?image=");
	        exporter.setParameter(JRHtmlExporterParameter.SIZE_UNIT, "px");
	        
	        return exporter;
        } catch(Exception ex){ 
        	ex.printStackTrace();
        	throw ex;
        	
        }finally {
        	ResourceManager.freeConnection(con ,TRACE_ID);
        }
	}
	
	/**
     * This method is used to generate the CSV Report using Jasper Report
     *
     * @param map - Parameter for the Query to pass to Report
     * @param reportFile - File object containing jasper file.
     * @param out - Print Writer Objects
     * @param request - Request Objects
     * @return - Returns JRCsvExporter Object.
     * @throws Exception
     */
    public JRCsvExporter generateCSVReport(HashMap<String ,Object> map, File reportFile ,HttpServletResponse response ,HttpServletRequest request) throws Exception{
       
      
        String reportFileName = reportFile.getName();
        reportFileName = reportFileName.substring(0,reportFileName.indexOf("."));
        reportFileName = reportFileName + System.currentTimeMillis();
        JasperReport jasperReport;
        JasperPrint jasperPrint;
        Connection con = null;
        JRCsvExporter exporter = new JRCsvExporter();
        HashMap imagesMap = new HashMap();
       
        try {
            con = ResourceManager.getConnection();
           
            jasperReport = (JasperReport)JRLoader.loadObject(reportFile);
            jasperPrint = JasperFillManager.fillReport(jasperReport, map ,con);
           
            request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_LIST_SESSION_ATTRIBUTE, jasperPrint);
            request.getSession().setAttribute("IMAGES_MAP", imagesMap);
           
            response.setContentType("application/vnd.ms-excel");

            exporter.setParameter(JRCsvExporterParameter.FIELD_DELIMITER,",'");
            exporter.setParameter(JRCsvExporterParameter.JASPER_PRINT,jasperPrint);
            exporter.setParameter(JRCsvExporterParameter.OUTPUT_WRITER,response.getWriter());
            response.setHeader("Content-Disposition", "attachment; filename="+reportFileName+".csv");
            
           
            return exporter;
        } catch(Exception ex){ 
        	ex.printStackTrace();
        	throw ex;
        	
        }finally {
            ResourceManager.freeConnection(con ,TRACE_ID);
        }
    }
    
    /**
     * This method is used to generate the CSV Report using Jasper Report
     *
     * @param map - Parameter for the Query to pass to Report
     * @param reportFile - File object containing jasper file.
     * @param out - Print Writer Objects
     * @param request - Request Objects
     * @param subReportFile - File object containing sub report jasper file.
     * @return - Returns JRCsvExporter Object.
     * @throws Exception
     */
    public JRCsvExporter generateCSVReportWithSubReport(HashMap<String ,Object> map, File reportFile ,HttpServletResponse response ,HttpServletRequest request ,File subReportFile) throws Exception{
       
      
        String reportFileName = reportFile.getName();
        reportFileName = reportFileName.substring(0,reportFileName.indexOf("."));
        reportFileName = reportFileName + System.currentTimeMillis();
        JasperReport jasperReport;
        JasperReport subReport;
        JasperPrint jasperPrint;
        Connection con = null;
        JRCsvExporter exporter = new JRCsvExporter();
        HashMap imagesMap = new HashMap();
       
        try {
            con = ResourceManager.getConnection();
           
            jasperReport = (JasperReport)JRLoader.loadObject(reportFile);
            subReport= (JasperReport)JRLoader.loadObject(subReportFile);
	        map.put("SubReport", subReport);
            jasperPrint = JasperFillManager.fillReport(jasperReport, map ,con);
           
            request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_LIST_SESSION_ATTRIBUTE, jasperPrint);
            request.getSession().setAttribute("IMAGES_MAP", imagesMap);
           
            response.setContentType("application/vnd.ms-excel");

            exporter.setParameter(JRCsvExporterParameter.FIELD_DELIMITER,",'");
            exporter.setParameter(JRCsvExporterParameter.JASPER_PRINT,jasperPrint);
            exporter.setParameter(JRCsvExporterParameter.OUTPUT_WRITER,response.getWriter());
            response.setHeader("Content-Disposition", "attachment; filename="+reportFileName+".csv");
            
           
            return exporter;
        } catch(Exception ex){ 
        	ex.printStackTrace();
        	throw ex;
        	
        }finally {
            ResourceManager.freeConnection(con ,TRACE_ID);
        }
    }
}